//
//  HJPersonalCenterController.m
//  KaiChat
//
//  Created by lx on 2023/10/18.
//

#import "HJPersonalCenterController.h"
#import "HJTitleCell.h"
#import "HJSectionHeaderView.h"
#import "HJPersonalCenterAvatarCell.h"
#import "HJModifyNicknameController.h"
#import "HJModifyIntroduceController.h"
#import "HJModifyUIDController.h"
#import "HJUserModel.h"
#import "HJModifyGenderController.h"
#import "HJMinePresent.h"
#import "HJPersonalAvatarController.h"
#import "HJSetBirthdayController.h"
#import "HJRegionController.h"

@interface HJPersonalCenterController ()

@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) HJUserModel *userModel;

@end

@implementation HJPersonalCenterController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _userModel = [HJUserModel currentAccountModel];
    
    self.navigationBar.title = @"Personal Information";
    self.view.backgroundColor = UIColor.whiteColor;
    self.tableViewStyle = UITableViewStylePlain;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.1)];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TabbarSafeBottomMargin, 0);
    [self.tableView registerClass:HJTitleCell.class forCellReuseIdentifier:@"HJTitleCell"];
    [self.tableView registerClass:HJPersonalCenterAvatarCell.class forCellReuseIdentifier:@"HJPersonalCenterAvatarCell"];
    [self.tableView registerClass:HJSectionHeaderView.class forHeaderFooterViewReuseIdentifier:@"HJSectionHeaderView"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 60;
    self.tableView.backgroundColor = UIColor.whiteColor;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *datas = self.datas[section];
    return [datas count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCellModel *model = self.datas[indexPath.section][indexPath.row];
    if ([model.identifier isEqualToString:@"avatar"]) {
        HJPersonalCenterAvatarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJPersonalCenterAvatarCell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    
    HJTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJTitleCell" forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    NSArray *sectionDatas = self.datas[section];
    return sectionDatas.count == 0 ? CGFLOAT_MIN : 20;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    HJSectionHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HJSectionHeaderView"];
    if (section == 0) {
        view.titleLabel.text = @"Basic Information";
    } else {
        view.titleLabel.text = @"Personal Information";
    }
    view.contentView.backgroundColor = UIColor.tj_F6F6F6Color;
    view.backgroundColor = UIColor.tj_F6F6F6Color;
    view.titleLabel.textColor = UIColor.tj_BEBDCAColor;
    view.titleLabel.font = UIFontMake(12);
    return view;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColor.clearColor;
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HJCellModel *model = self.datas[indexPath.section][indexPath.row];
    TJWeakify(self)
    if ([model.identifier isEqualToString:@"nickname"]) {
        HJModifyNicknameController *modifyNicknameController = [[HJModifyNicknameController alloc] init];
        [modifyNicknameController setComplete:^{
            model.detailText = HJUserInfo.nickname;
            [weakself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }];
        [modifyNicknameController showInController:self];
        return;
    }
    
    if ([model.identifier isEqualToString:@"whatsUp"]) {
        HJModifyIntroduceController *modifyIntroduceController = [[HJModifyIntroduceController alloc] init];
        modifyIntroduceController.whatsUp = self.userModel.whatsUp;
        [modifyIntroduceController setComplete:^(NSString *value){
            model.detailText = value;
            weakself.userModel.whatsUp = value;
            NSString *whatsUp = self.userModel.whatsUp;
            if (whatsUp.length == 0) {
                model.detailText = @"Briefly introduce yourself...";
            } else {
                model.detailText = whatsUp;
            }
            [weakself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }];
        [self.navigationController pushViewController:modifyIntroduceController];
        return;
    }
    
    if ([model.identifier isEqualToString:@"UID"]) {
        HJModifyUIDController *modifyUIDController = [[HJModifyUIDController alloc] init];
        modifyUIDController.userModel = self.userModel;
        [modifyUIDController setComplete:^(NSString *value){
            model.detailText = value;
            [weakself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            [weakself loadData];
        }];
        [self.navigationController pushViewController:modifyUIDController];
        return;
    }
    
    if ([model.identifier isEqualToString:@"avatar"]) {
        HJPersonalAvatarController *personalAvatarController = [[HJPersonalAvatarController alloc] init];
        [personalAvatarController setComplete:^(NSString * _Nonnull value) {
            weakself.userModel.avatar = value;
            HJUserModel *userModel = [HJUserModel currentAccountModel];
            userModel.avatar = value;
            [userModel bg_saveOrUpdateAsync:nil];            
            model.detailText = value;
        }];
        [self.navigationController pushViewController:personalAvatarController];
        return;
    }

    if ([model.identifier isEqualToString:@"gender"]) {
        HJModifyGenderController *modifyGenderController = [[HJModifyGenderController alloc] init];
        modifyGenderController.type = model.detailText;
        [modifyGenderController setComplete:^(HJCellModel *cellModel){
            weakself.userModel.gender = cellModel.anyObject;
            model.detailText = cellModel.text;
            model.detailTextColor = UIColor.tj_868391Color;
            [weakself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }];
        [self presentViewController:modifyGenderController animated:NO completion:nil];
        return;
    }
    
    if ([model.identifier isEqualToString:@"birthday"]) {
        HJSetBirthdayController *setBirthdayController = [[HJSetBirthdayController alloc] init];
        setBirthdayController.date = self.userModel.birthday;
        [setBirthdayController setComplete:^(NSString * _Nonnull value) {
            weakself.userModel.birthday = value;
            model.detailText = value;
            model.detailTextColor = UIColor.tj_868391Color;
            [weakself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }];
        [self presentViewController:setBirthdayController animated:NO completion:nil];
        return;
    }
    
    if ([model.identifier isEqualToString:@"region"]) {
        HJRegionController *regionController = [[HJRegionController alloc] init];
        regionController.region = self.userModel.region;
        [regionController setComplete:^(NSString * _Nonnull value) {
            weakself.userModel.region = value;
            model.detailText = value;
            model.detailTextColor = UIColor.tj_868391Color;
            [weakself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }];
        [self.navigationController pushViewController:regionController];
        return;
    }
    
}

- (void)loadData {
    [HJMinePresent loadUserInfoComplete:^(HJUserModel * _Nullable model) {
        self.userModel = model;
        self.datas = nil;
        [self.tableView reloadData];
        NSLog(@"self.userModel = %@", self.userModel);
    }];
}

- (NSArray *)datas {
    if (!_datas) {
        _datas = @[
            @[
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.text = @"Profile Photo";
                    model.identifier = @"avatar";
                    model.detailText = self.userModel.avatar;
                }],
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.text = @"Nickname";
                    model.showArrow = YES;
                    model.identifier = @"nickname";
                    model.detailText = self.userModel.nickname;
                    model.detailTextColor = UIColor.tj_868391Color;
                }],
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.text = @"UID";
                    model.identifier = @"UID";
                    model.detailText = self.userModel.uid;
                    model.detailTextColor = UIColor.tj_868391Color;
                }],
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.text = @"What’s up";
                    model.identifier = @"whatsUp";
                    model.detailTextColor = UIColor.tj_868391Color;

                    NSString *whatsUp = self.userModel.whatsUp;
                    if (whatsUp.length == 0) {
                        model.detailText = @"Briefly introduce yourself...";
                    } else {
                        model.detailText = whatsUp;
                    }
                }],
            ],
            @[
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.text = @"Gender";
                    model.detailText = @"Not Set";
                    model.identifier = @"gender";
                    NSInteger gender = [self.userModel.gender intValue];
                    if (gender == 0) {
                        model.detailText = @"Not Set";
                        model.detailTextColor = UIColor.tj_BEBDCAColor;
                    } else if (gender == 1) {
                        model.detailText = @"Male";
                        model.detailTextColor = UIColor.tj_868391Color;
                    } else if (gender == 2) {
                        model.detailText = @"Female";
                        model.detailTextColor = UIColor.tj_868391Color;
                    } else {
                        model.detailText = @"Secrecy";
                        model.detailTextColor = UIColor.tj_868391Color;
                    }
                }],
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.text = @"Birthday";
                    model.identifier = @"birthday";
                    NSString *birthday = self.userModel.birthday;
                    if (isEmptyString(birthday)) {
                        model.detailText = @"Not Set";
                        model.detailTextColor = UIColor.tj_BEBDCAColor;
                    } else {
                        model.detailText = birthday;
                        model.detailTextColor = UIColor.tj_868391Color;
                    }
                }],
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.text = @"Region";
                    model.identifier = @"region";
                    NSString *region = self.userModel.region;
                    if (isEmptyString(region)) {
                        model.detailText = @"Not Set";
                        model.detailTextColor = UIColor.tj_BEBDCAColor;
                    } else {
                        model.detailText = region;
                        model.detailTextColor = UIColor.tj_868391Color;
                    }
                }],
            ],
            
        ];
    }
    return _datas;
}

- (void)dealloc {
    TJLog(@"%s", __func__);
}
 

@end
